$(function () {
    var baseUrl = window.location.protocol + "//" + window.location.host;

    layui.use(['layer','layedit','jquery'], function () {
        //富文本编辑器
        var $ = layui.jquery
            , layer = layui.layer
            , layedit = layui.layedit;
        layedit.set({
            height: '80%'
        });
        var ieditor = layedit.build('answerWrite',{
            tool: [
                'strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线

                ,'|' //分割线

                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'link' //超链接
                ,'unlink' //清除链接
                ,'face' //表情
                ,'help' //帮助
            ]
        });

        //回答
        $('.aSubmit').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if(layedit.getContent(ieditor)==''){ return false; }

            if($('input[name=problem_id]')==''){ layer.msg('问题不存在！', {icon: 5,time: 2000}); return false; }

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                problem_id:$('input[name=problem_id]').val(),
                content: layedit.getContent(ieditor),
            };
            $.ajax({
                type: "POST",
                url: baseUrl+"/api/answer/add",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        //评论
        $('.cSubmit').on('click',function () {
            if($(this).attr('data-status')==1) return false;
            if($(this).parent().find('textarea').val()==''){ return false; }

            if($(this).attr('data-answer_id')==''){ layer.msg('回答不存在！', {icon: 5,time: 2000}); return false; }

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                content:$(this).parent().find('textarea').val(),
                answer_id: $(this).attr('data-answer_id'),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/comment/add",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        //点击评论
        $('.click-comment').on('click',function () {
            if($(this).parent().parent().parent().find('.commentContainer').css('display')=='none'){
                $(this).parent().parent().parent().find('.commentContainer').slideDown();
            }else{
                $(this).parent().parent().parent().find('.commentContainer').slideUp();
            }
        });

        //提问
        $('#pSubmit').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if($('input[name=keywords]').val()==''){ $('input[name=keywords]').css('border','1px solid #f00');  return false; }
            $('input[name=keywords]').css('border','1px solid #00C1DE');

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                content:$('input[name=keywords]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/problem/add",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        //搜索
        $('#sSubmit').on('click',function () {
            if($('input[name=keywords]').val()==''){ $('input[name=keywords]').css('border','1px solid #f00');  return false; }
            $('input[name=keywords]').css('border','1px solid #00C1DE');

            window.location.href=baseUrl+'/quesans/'+$('input[name=keywords]').val();
        });


    });
});